import store from '@/store/index.js'
import Vue from 'vue'



var n = 0,
	m = 0;
var Fly = require("flyio/dist/npm/wx");
var request = new Fly();
Vue.prototype.$fly = request
Vue.prototype.$baseUrl = 'https://interface.2855855.com' // 二期线上接口
// Vue.prototype.$baseUrl = 'https://test1.2855855.com' // 二期体验版


request.interceptors.request.use((request) => {
	const token = uni.getStorageSync('token') || store.getters['getToken'] || '';
	// console.log('token', token); // 测试token，上线删除
	request.headers["token"] = token;
	// uni.showLoading({
	// 	title: '正在努力加载中...'
	// });

	// request.baseURL = 'https://interface.2855855.com';
	request.baseURL = Vue.prototype.$baseUrl;

	// 防止缓存
	if (request.method === 'POST' && request.headers['Content-Type'] !== 'multipart/form-data') {
		request.body = {
			...request.body,
		}
	} else if (request.method === 'GET') {
		request.params = {
			...request.params
		}
	}
	return request
})


request.interceptors.response.use(function(response) { //不要使用箭头函数，否则调用this.lock()时，this指向不对 
	// uni.hideLoading();
	let errmsg = '';
	const data = response.data;

	if (!data || typeof data !== 'object') {
		errmsg = '服务器响应格式错误';
		uni.showToast({
			title: errmsg,
			icon: 'none'
		})
	} else {
		let {
			code,
			msg
		} = data
		if (code == 2001 || code == 2026) { //判断code 2001 表示没有token,需要重新授权登录
			store.commit('upToken', '')
			m++
			if (m === 1) {
				uni.navigateTo({
					url: '/pages/others/login',
					success() {
						setTimeout(() => {
							m = 0
						}, 2000)
					}
				})
			}
		}
		if (code == 2028) {
			n++
			if (n === 1) {
				uni.navigateTo({
					url: '/pagesUser/bindPhone?type=1',
					success() {
						setTimeout(() => {
							n = 0
						}, 2000)
					}
				});
			}
		}
		if (code == 2037) {
			n++
			if (n === 1) {
				uni.navigateTo({
					url: '/pagesUser/userNickname?type=2037',
					success() {
						setTimeout(() => {
							n = 0
						}, 2000)
					}
				});
			}
		}
		if (code == 2038) {
			n++
			if (n === 1) {
				uni.navigateTo({
					url: '/pagesUser/userInfo?type=2038',
					success() {
						setTimeout(() => {
							n = 0
						}, 2000)
					}
				});
			}
		}

		// 没有设置登录密码
		if (code == 2035) {
			uni.showToast({
				icon: 'none',
				title: msg
			})
			setTimeout(() => {
				uni.navigateTo({
					url: '/pagesUser/forgetPwd?edit=2'
				})
			}, 1000)
		}

		if (code > 2000 || code == 500) {
			uni.showToast({
				title: msg,
				icon: 'none'
			});
		}

	}
	return response.data; //只返回业务数据部分
}, function(err) {
	// console.log(err);
	let errmsg = err.message;
	switch (err.status) {
		case 0:
			errmsg = "网络连接错误";
			uni.showToast({
				title: errmsg,
				icon: 'none'
			})
			break;
		case 501:
			uni.navigateTo({
				url: '/pages/login/login'
			})
			break;
		default:
			uni.showToast({
				title: errmsg,
				icon: 'none'
			})
	}
})

export default request

function http(api, updata) {
	// if (store.getters.isLogin && !store.getters.getToken) { // false 已经登录或者不需要登录   // true 已经判断需要登录，不执行接下来的接口
	// 	return
	// }
	// store.commit('upIsLogin', true)
	let result = null
	request.post(api, updata).then((res) => {
		result = res
	}).finally(() => {
		// store.commit('upIsLogin', false)
	})
	return result
}
// export default http